package com.ckjw.gobang.utils;

import java.awt.Color;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JOptionPane;

import com.ckjw.gobang.data.GameCenter;
import com.ckjw.gobang.data.Player;
import com.ckjw.gobang.data.Spot;
import com.ckjw.gobang.data.TableData;
import com.ckjw.gobang.view.MainInterface;
import com.ckjw.gobang.view.UserPanel2;

/**
 * 设置倒计时的线程
 * 
 * @author lck and djw
 * @Date 2022年6月25日-下午8:44:26
 */
public class settimer extends Thread {
	@Override
	public void run() {
		while (!GameCenter.isEnd()) {
			if (TableData.nowColor.equals(Spot.blackChess)) {// 轮到黒棋倒计时
				for (int i = 30; i >= 0; i--) {
					try {
						Thread.sleep(1000);
						if (Player.onlineuser.getClor().equals(Spot.blackChess)) {//黒棋在左边
							if (TableData.nowColor.equals(Spot.whiteChess)|| GameCenter.isEnd()) {
								UserPanel2.lblNewLabel_4.setText(30 + " 秒");
								UserPanel2.lblNewLabel_4.setForeground(Color.black);
								break;
							}
							if (i==10) {
								UserPanel2.lblNewLabel_4.setForeground(Color.red);
							}
							UserPanel2.lblNewLabel_4.setText(i + " 秒");
							UserPanel2.userPanel.repaint();
							MainInterface.mainInterface.repaint();
						} else {//黒棋在右边
							if (TableData.nowColor.equals(Spot.whiteChess)|| GameCenter.isEnd()) {
								UserPanel2.lblNewLabel_5.setText(30 + " 秒");
								UserPanel2.lblNewLabel_5.setForeground(Color.black);
								break;
							}
							if (i==10) {
								UserPanel2.lblNewLabel_5.setForeground(Color.red);
							}
							UserPanel2.lblNewLabel_5.setText(i + " 秒");
							UserPanel2.userPanel.repaint();
							MainInterface.mainInterface.repaint();
						}

					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//倒计时结束
					if (i==0) {
						if (Player.onlineuser.getClor().equals(Spot.blackChess))//
						{
							JOptionPane.showMessageDialog(MainInterface.mainInterface, "您已超时，对局失败", "提示消息",
									JOptionPane.INFORMATION_MESSAGE);
							GameCenter.setMode(GameCenter.MODE_END);
						}
						else {
							JOptionPane.showMessageDialog(MainInterface.mainInterface, "对方超时，对局获胜", "提示消息",
									JOptionPane.INFORMATION_MESSAGE);
							GameCenter.setMode(GameCenter.MODE_END);
						}
						resettimer.resettimeui();
					}
				}

			} else {// 轮到白棋倒计时
				for (int i = 30; i >= 0; i--) {
					try {
						Thread.sleep(1000);
						if (Player.onlineuser.getClor().equals(Spot.blackChess)) {//黒棋在左边
							if (TableData.nowColor.equals(Spot.blackChess)|| GameCenter.isEnd()) {
								UserPanel2.lblNewLabel_5.setText(30 + " 秒");
								UserPanel2.lblNewLabel_5.setForeground(Color.black);
								break;
							}
							if (i==10) {
								UserPanel2.lblNewLabel_5.setForeground(Color.red);
							}
							UserPanel2.lblNewLabel_5.setText(i + " 秒");
							UserPanel2.userPanel.repaint();
							MainInterface.mainInterface.repaint();
						} else {//黒棋在右边
							if (TableData.nowColor.equals(Spot.blackChess)|| GameCenter.isEnd()) {
								UserPanel2.lblNewLabel_4.setText(30 + " 秒");
								UserPanel2.lblNewLabel_4.setForeground(Color.black);
								break;
							}
							if (i==10) {
								UserPanel2.lblNewLabel_4.setForeground(Color.red);
							}
							UserPanel2.lblNewLabel_4.setText(i + " 秒");
							UserPanel2.userPanel.repaint();
							MainInterface.mainInterface.repaint();
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//倒计时结束
					if (i==0) {
						if (Player.onlineuser.getClor().equals(Spot.whiteChess))//
						{
							JOptionPane.showMessageDialog(MainInterface.mainInterface, "您已超时，对局失败", "提示消息",
									JOptionPane.INFORMATION_MESSAGE);
							GameCenter.setMode(GameCenter.MODE_END);
						}
						else {
							JOptionPane.showMessageDialog(MainInterface.mainInterface, "对方超时，对局获胜", "提示消息",
									JOptionPane.INFORMATION_MESSAGE);
							GameCenter.setMode(GameCenter.MODE_END);
						}
						resettimer.resettimeui();
					}
				}
			}
		}
	}
}
